<?php
 header("content-type:text/html;charset=utf-8");
include("Template.php");

$table=  @$_POST["table"];
$trueTableName=ReplaceTableName( $table,$DbPrefix);

echo TableList($db) ;   

function TableList($tablename)
{
   $tbllist="<form method=\"post\"><select id=\"table\" name=\"table\">";
   $rs= mysql_list_tables($tablename);
   
   while($row=mysql_fetch_row($rs))
   {
       
      $tbllist.="<option value=\"{$row[0]}\">{$row[0]}</option>\r";   
   }
   
   return $tbllist."</select><input type=\"submit\" value=\"生成\"></form>";
}









if($table){
echo "<textarea cols=\"120\" rows=\"80\">".TemplateReplace('Form',$trueTableName,$ModelTemplate)."</textarea>";

 //生成插入语句
 $map= ModelInsertSql($table);
 
 $tempInserSql=$ModelInserSql;
  $tempInserSql= TemplateReplace("TableName",$table,$tempInserSql); 
 $tempInserSql= TemplateReplace("FieldsName",$map['FieldsName'],$tempInserSql);  
 $tempInserSql=   TemplateReplace("FieldsValue",$map['FieldsValue'],$tempInserSql); 
 //结束生成插入
 
 
  //生成更新语句
 $map= ModelUpdateSql($table);
 
 $tempUpdateSql=$ModelUpdateSql;
  $tempUpdateSql= TemplateReplace("TableName",$table,$tempUpdateSql); 
 $tempUpdateSql= TemplateReplace("UpdateFields",$map,$tempUpdateSql);  

 //结束生成插入


  $tempReplace=$ModelTemplate;
 $tempReplace=TemplateReplace('Form',$trueTableName,$tempReplace); 
 $tempReplace=TemplateReplace('TableName',$table,$tempReplace);
 $tempReplace=TemplateReplace('DataBaseName',$db,$tempReplace);
 $FieldMapTemplate=TemplateReplace('FieldMap',FieldMap($table),$FieldMapTemplate); 
  $tempReplace=TemplateReplace('Map',$FieldMapTemplate,$tempReplace);
 $tempReplace=TemplateReplace('Validate',FormValidate($table,$FormValidateTemplate),$tempReplace); 
  $tempReplace=TemplateReplace('Auto',FormAuto($table,$FormAutoTemplate),$tempReplace); 
  $tempReplace=TemplateReplace('ModelInserSql',$tempInserSql,$tempReplace); 
  $tempReplace=TemplateReplace('ModelUpdateSql',$tempUpdateSql,$tempReplace); 
 
 
 WriteFile($ModelPath.$trueTableName."Model.class.php",TemplateReplace('Form',$trueTableName,$tempReplace));





 
 if(!  file_exists($ModelPath."CommonModel.class.php"))
{
    WriteFile($ModelPath."CommonModel.class.php",$CommonModelTemplate);
}
 
}

?>